from django.db import models
from user.models import UserInfo


class Base(models.Model):
    # 是否注销
    is_delete = models.BooleanField(default=False)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        abstract = True


IS_ONLY = ((1, 'Y'), (2, 'N'))


class AttributeValue(Base):
    name = models.CharField(max_length=32, verbose_name='属性类型名称')
    attribute = models.ForeignKey(to='Attribute', on_delete=models.DO_NOTHING)
    is_choose = models.BooleanField(default=True)
    stock = models.IntegerField(verbose_name='含有商品属性库存数量')
    
    class Meta:
        verbose_name = '商品属性值表'
        verbose_name_plural = verbose_name


class Attribute(Base):
    name = models.CharField(max_length=32, verbose_name='属性名称')
    is_only = models.IntegerField(choices=IS_ONLY, default=1)
    is_required = models.BooleanField(default=False)
    goods = models.ForeignKey(to='Goods', on_delete=models.CASCADE)
    
    class Meta:
        verbose_name = '商品属性表'
        verbose_name_plural = verbose_name


class GoodsGallery(Base):
    name = models.CharField(max_length=32, verbose_name='相册名称')
    desc = models.TextField(verbose_name='商品相册描述')
    big_picture = models.ImageField(upload_to='GoodsGallery/', default='GoodsGallery/default1.jpg',
                                    verbose_name='商品相册图片')
    goods = models.ForeignKey(to='Goods', on_delete=models.CASCADE)
    
    class Meta:
        verbose_name = '商品相册表'
        verbose_name_plural = verbose_name


class Store(Base):
    name = models.CharField(max_length=32, verbose_name='店铺名称')
    desc = models.TextField(verbose_name='店铺简介')

class Goods(Base):
    title = models.CharField(max_length=32, verbose_name='商品名称')
    desc = models.TextField(verbose_name='商品描述')
    plural_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品原价')
    sale_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品出售价')
    has_attribute = models.BooleanField(default=True, verbose_name='是否含有属性')
    is_active = models.BooleanField(default=False, verbose_name='是否参与活动(秒杀)')
    sort_number = models.IntegerField(verbose_name='排序')
    stock = models.IntegerField(verbose_name='无商品属性库存数量')
    goods_no = models.CharField(max_length=60, verbose_name='货号')
    remark = models.CharField(max_length=255, verbose_name='备注')
    popularity = models.BigIntegerField(default=0, verbose_name='人气')
    collect = models.BigIntegerField(default=0, verbose_name='收藏')
    level = models.SmallIntegerField(verbose_name='商品等级')
    total_sale = models.BigIntegerField(verbose_name='历史卖出数量')
    contact = models.OneToOneField(to='GoodsContact', on_delete=models.DO_NOTHING)
    picture = models.ImageField(upload_to='Goods/', default='Goods/default.jpg')
    brand = models.ForeignKey(to='GoodsBrands', on_delete=models.DO_NOTHING)
    store = models.ForeignKey(to='Store', on_delete=models.DO_NOTHING, null=True)




class GoodsContact(Base):
    username = models.CharField(max_length=32, verbose_name='商品联系人')
    phone = models.CharField(max_length=32, verbose_name='联系人手机号')
    address = models.CharField(max_length=255, verbose_name='联系人地址')
    
    class Meta:
        verbose_name = '商品联系人'
        verbose_name_plural = verbose_name


# 商品品类表

class GoodsCategory(Base):
    sorted_num = models.IntegerField(verbose_name='自定义排序')
    name = models.CharField(max_length=32, verbose_name='商品分类名')
    parent_id = models.ForeignKey(to='self', on_delete=models.CASCADE)


# 商品品牌表
class GoodsBrands(Base):
    name = models.CharField(max_length=32, verbose_name='品牌名')
    logo = models.FileField(upload_to='BrandsLogo/', default='BrandsLogo/default.png')
    desc = models.TextField(verbose_name='品牌简介')
    sorted_num = models.IntegerField(verbose_name='自定义排序')
    category = models.ManyToManyField(to='BrandsCategory', db_constraint=False)

    class Meta:
        verbose_name_plural = '商品品牌表'

    def __str__(self):
        return self.name

    def category_name(self):
        categorys= ','.join([i.name for i in self.category.all()])
    
        return categorys

    def category_id(self):
        self.category.objects.filter()


class BrandsCategory(Base):
    sorted_num = models.IntegerField(verbose_name='自定义排序')
    
    name = models.CharField(max_length=32, verbose_name='品牌分类名')


# 商品评论表
class GoodsComment(Base):
    # 用户与评论多对多关系
    user = models.ForeignKey(to=UserInfo, on_delete=models.CASCADE, verbose_name='关联用户')
    is_super = models.BooleanField(verbose_name='是否是会员')
    # 购买的商品属性 规格 大小等
    goods = models.ForeignKey(to='Goods', on_delete=models.CASCADE, verbose_name='关联商品')
    comment_time = models.DateTimeField(auto_now_add=True, verbose_name='评论时间')
    # 评分  根据评分筛选是否是 优中差评论
    score = models.IntegerField(verbose_name='评分')
    content = models.TextField(verbose_name='评论')
    images = models.FileField(upload_to="CommentImage/", null=True, verbose_name='评论是否有图片')
    comment_id = models.ForeignKey(to='self', on_delete=models.CASCADE, null=True, verbose_name='父评论')
    like_num = models.IntegerField(verbose_name='评论点赞数')
    
    def __str__(self):
        return '商品评论'
    
    class Meta:
        verbose_name_plural = '商品评论'


class CommentLike(Base):
    user = models.ForeignKey(to=UserInfo, on_delete=models.CASCADE)
    comment = models.ForeignKey(to='GoodsComment', on_delete=models.CASCADE)
    like = models.BooleanField(verbose_name='点赞')
    like_time = models.DateTimeField(auto_now_add=True, verbose_name='点赞时间')
    
    class Meta:
        verbose_name_plural = '点赞'


